package com.laf.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laf.common.constant.HttpStatus;
import com.laf.common.core.page.TableDataInfo;
import com.laf.common.exception.ServiceException;
import com.laf.common.utils.SecurityUtils;
import com.laf.common.utils.StringUtils;
import com.laf.manage.domain.doc.ReceiveRecordDoc;
import com.laf.manage.domain.dto.PageDTO;
import com.laf.manage.domain.po.LostItem;
import com.laf.manage.domain.po.ReceiveRecord;
import com.laf.manage.domain.query.ReceiveRecordQuery;
import com.laf.manage.domain.vo.ReceiveRecordVO;
import com.laf.manage.domain.vo.RecordDetailsVO;
import com.laf.manage.mapper.es.ReceiveRecordEsMapper;
import com.laf.manage.mapper.mp.ReceiveRecordMapper;
import com.laf.manage.service.ILostItemService;
import com.laf.manage.service.IReceiveRecordService;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 领取记录Service业务层处理
 *
 * @author zian
 * @date 2024-11-01
 */
@Service
public class ReceiveRecordServiceImpl extends ServiceImpl<ReceiveRecordMapper, ReceiveRecord> implements IReceiveRecordService {

    @Autowired
    private ReceiveRecordMapper receiveRecordMapper;

    @Autowired
    private ILostItemService lostItemService;

    @Autowired
    private ReceiveRecordEsMapper receiveRecordEsMapper;

    /**
     * 查询领取记录列表
     *
     * @param receiveRecord 领取记录
     * @return 领取记录
     */
    @Override
    public List<ReceiveRecord> selectReceiveRecordList(ReceiveRecord receiveRecord) {
        List<ReceiveRecord> receiveRecordList = receiveRecordMapper.selectList(buildQueryWrapper(receiveRecord));
        return receiveRecordList;
    }

    @Override
    public TableDataInfo selectReceiveRecordList(ReceiveRecord receiveRecord, Integer pageNum, Integer pageSize) {
        EsPageInfo<ReceiveRecordDoc> receiveRecordDocEsPageInfo = receiveRecordEsMapper.pageQuery(buildEsQueryWrapper(receiveRecord), pageNum, pageSize);
        TableDataInfo tableDataInfo = new TableDataInfo();
        List<ReceiveRecordDoc> list = receiveRecordDocEsPageInfo.getList();
        if (CollUtil.isEmpty(list)) TableDataInfo.empty();

        tableDataInfo.setTotal(receiveRecordDocEsPageInfo.getTotal());
        tableDataInfo.setRows(list);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    private LambdaEsQueryWrapper<ReceiveRecordDoc> buildEsQueryWrapper(ReceiveRecord receiveRecord) {
        LambdaEsQueryWrapper<ReceiveRecordDoc> wrapper = new LambdaEsQueryWrapper<>();
        // 构建查询条件
        // 点位
        wrapper.eq(receiveRecord.getPointId() != null, ReceiveRecordDoc::getPointId, receiveRecord.getPointId());
        // 分类
        wrapper.eq(receiveRecord.getCategoryId() != null, ReceiveRecordDoc::getCategoryId, receiveRecord.getCategoryId());
        // 审核人
        wrapper.eq(StrUtil.isNotBlank(receiveRecord.getCreateBy()), ReceiveRecordDoc::getCreateBy, receiveRecord.getCreateBy());
        // 领取人真实姓名
        if (StringUtils.isNotBlank(receiveRecord.getRealName())) {
            wrapper.like(ReceiveRecordDoc::getRealName, receiveRecord.getRealName());
        }

        // 关键字 ： itemName 、 pickUpLocation
        StringBuilder keyword = new StringBuilder();
        if (StringUtils.isNotBlank(receiveRecord.getItemName())) {
            keyword.append(receiveRecord.getItemName());
        }
        if (StringUtils.isNotBlank(receiveRecord.getPickUpLocation())) {
            keyword.append(receiveRecord.getPickUpLocation());
        }
        if (StringUtils.isNotBlank(keyword.toString())) {
            wrapper.multiMatchQuery(keyword.toString(), ReceiveRecordDoc::getItemName, ReceiveRecordDoc::getPickUpLocation);
        }
        wrapper.orderByDesc(ReceiveRecordDoc::getCreateTime);
        return wrapper;
    }

    @Override
    public ReceiveRecordVO getInfoById(Long id) {
        ReceiveRecord receiveRecord = getById(id);
        if (receiveRecord == null) throw new ServiceException("记录不存在");
        ReceiveRecordVO vo = BeanUtil.copyProperties(receiveRecord, ReceiveRecordVO.class);
        LostItem lostItem = lostItemService.getById(vo.getItemId());
        if (lostItem == null) {
            throw new ServiceException("记录不存在");
        }
        vo.setReporter(lostItem.getCreateBy());
        return vo;
    }


    private LambdaQueryWrapper<ReceiveRecord> buildQueryWrapper(ReceiveRecord query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<ReceiveRecord> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getItemName()), ReceiveRecord::getItemName, query.getItemName());
        lqw.eq(StringUtils.isNotBlank(query.getPickUpLocation()), ReceiveRecord::getPickUpLocation, query.getPickUpLocation());
        lqw.eq(query.getCategoryId() != null, ReceiveRecord::getCategoryId, query.getCategoryId());
        lqw.eq(query.getPointId() != null, ReceiveRecord::getPointId, query.getPointId());
        lqw.like(StringUtils.isNotBlank(query.getRealName()), ReceiveRecord::getRealName, query.getRealName());
        lqw.orderByDesc(ReceiveRecord::getCreateTime);
        return lqw;
    }


    @Override
    public PageDTO<ReceiveRecordDoc> getCurrentReceiveRecordList(ReceiveRecordQuery query) {
        // 补充查询条件
        Long userId = SecurityUtils.getMobileUserId();
        query.setCreateUserId(userId);

        // 执行查询
        EsPageInfo<ReceiveRecordDoc> esPageInfo = receiveRecordEsMapper.pageQuery(builderQuery(query), query.getPageNo(), query.getPageSize());
        List<ReceiveRecordDoc> list = esPageInfo.getList();
        if (CollUtil.isEmpty(list)) {
            return PageDTO.empty(0L, 0L);
        }

        // 封装 dto 对象返回
        return PageDTO.of(esPageInfo.getTotal(), (long) esPageInfo.getPages(), list);
    }

    /**
     * 领取记录详情
     *
     * @param id id
     * @return
     */
    @Override
    public RecordDetailsVO getReceiveRecordDetails(Long id) {
        return getBaseMapper().selectReceiveRecordDetails(id);
    }

    @Override
    public ReceiveRecordVO getInfoByItemId(Long itemId) {
        return getBaseMapper().selectReceiveRecordDetailsByItemId(itemId);
    }


    /**
     * 构建查询条件
     */
    public LambdaEsQueryWrapper<ReceiveRecordDoc> builderQuery(ReceiveRecordQuery query) {
        LambdaEsQueryWrapper<ReceiveRecordDoc> wrapper = new LambdaEsQueryWrapper<>();

        // 1.分类
        wrapper.eq(query.getCategoryId() != null, ReceiveRecordDoc::getCategoryId, query.getCategoryId());
        // 2. 点位
        wrapper.eq(query.getLocationId() != null, ReceiveRecordDoc::getPointId, query.getLocationId());
        // 3. 领取人id
        wrapper.eq(ReceiveRecordDoc::getUserId, query.getCreateUserId());
        // 关键字
        if (StrUtil.isNotBlank(query.getKey())) {
            wrapper.multiMatchQuery(query.getKey(), ReceiveRecordDoc::getItemName, ReceiveRecordDoc::getPickUpLocation);
        }
        // 排序
        wrapper.orderByDesc(ReceiveRecordDoc::getCreateTime);
        return wrapper;
    }


}
